﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for DA_FinanceYear
/// </summary>
public class DA_FinanceYear
{
    AuditBillingEntities objABEnt;
    public DA_FinanceYear()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    public List<PLFinanceYear> DA_fnGetFinanceYearList(PLFinanceYear pObjFinYear)
    {
        using (objABEnt = new AuditBillingEntities())
        {
            var varRes = (from i in objABEnt.FinancialYears
                          where (string.IsNullOrEmpty(pObjFinYear.SearchFinYearName) || i.FinYearName.Contains((pObjFinYear.SearchFinYearName))) &&
                          (pObjFinYear.SearchIsClosed == null || pObjFinYear.SearchIsClosed == i.IsCurrentFinYear)
                          orderby i.FinYearStartDate
                          select new PLFinanceYear()
                          {
                              FinancialYearID = i.FinancialYearID,
                              FinYearName = i.FinYearName,
                              FinYearStartDate = i.FinYearStartDate,
                              FinYearEndDate = i.FinYearEndDate,
                              IsFinYearClosed = i.IsFinYearClosed,
                              IsCurrentFinYear = i.IsCurrentFinYear
                          });
            return varRes.ToList();
        }
    }

    public List<PLFinanceYear> DA_fnGetFinanceYears(PLFinanceYear pObjFinYear)
    {
        using (objABEnt = new AuditBillingEntities())
        {
            var varRes = (from i in objABEnt.FinancialYears
                          where
                          i.IsFinYearClosed == true && i.IsCurrentFinYear == false
                          select new PLFinanceYear()
                          {
                              FinancialYearID = i.FinancialYearID,
                              FinYearName = i.FinYearName
                          });
            return varRes.ToList();
        }
    }

    public PLFinanceYear DA_fnGetCurrentFinancialYear()
    {
        using (objABEnt = new AuditBillingEntities())
        {
            var varRes = (from i in objABEnt.FinancialYears
                          where
                          i.IsFinYearClosed == false && i.IsCurrentFinYear == true
                          select new PLFinanceYear()
                          {
                              FinancialYearID = i.FinancialYearID,
                              FinYearStartDate = i.FinYearStartDate,
                              FinYearEndDate = i.FinYearEndDate
                          });
            return varRes.FirstOrDefault();
        }
    }

    public string DA_fnGetCompletedContractDetails()
    {
        using (objABEnt = new AuditBillingEntities())
        { 
            string strVals = string.Join("~", objABEnt.TrnContracts.Where(s => (s.FinancialYearID ?? 0) == 0 && s.IsCompleted == true)
                                   .Select(s => s.TrnContractID));
             
            return strVals;
        }
    }


    public int DA_InsertUpdateFinancialYear(PLFinanceYear pObjFinYear)
    {
        using (objABEnt = new AuditBillingEntities())
        {
            if (pObjFinYear.FinYearStartDate >= pObjFinYear.FinYearEndDate)
                return -1;

            var varNameDupl = (from i in objABEnt.FinancialYears
                               where i.FinYearName == pObjFinYear.FinYearName && i.FinancialYearID != pObjFinYear.FinancialYearID
                               select i);
            if (varNameDupl.Count() > 0)
                return -2;

            var varDateDupl1 = (from i in objABEnt.FinancialYears
                                where i.FinancialYearID != pObjFinYear.FinancialYearID &&
                                ((pObjFinYear.FinYearStartDate >= i.FinYearStartDate && pObjFinYear.FinYearStartDate <= i.FinYearEndDate) ||
                                (pObjFinYear.FinYearEndDate >= i.FinYearStartDate && pObjFinYear.FinYearEndDate <= i.FinYearEndDate))
                                select i);
            if (varDateDupl1.Count() > 0)
                return -3;

            FinancialYear objFinYear;
            if (pObjFinYear.FinancialYearID == 0)
            {
                objFinYear = new FinancialYear();
                objFinYear.CreatedBy = SessionInfo.prop_getObj.EmployeeID;
                objFinYear.CreatedOn = DateTime.Now;
            }
            else
            {
                objFinYear = (from i in objABEnt.FinancialYears
                              where i.FinancialYearID == pObjFinYear.FinancialYearID
                              select i).SingleOrDefault();
                objFinYear.ModifiedBy = SessionInfo.prop_getObj.EmployeeID;
                objFinYear.ModifiedOn = DateTime.Now;
            }
            objFinYear.FinYearName = pObjFinYear.FinYearName;
            objFinYear.FinYearStartDate = pObjFinYear.FinYearStartDate;
            objFinYear.FinYearEndDate = pObjFinYear.FinYearEndDate;

            if (pObjFinYear.FinancialYearID == 0)
            {
                objABEnt.FinancialYears.Add(objFinYear);
            }
            objABEnt.SaveChanges();
            return objFinYear.FinancialYearID;
        }
    }
}